updating oE gets

gets

<built-in> function gets(integer fn) 

gets a sequence of characters.

Parameters:
  1. fn : an integer, the handle of the file or device to read from.
Returns:

An object, either EOF on end of file, or the next line of text from the file.

Errors:

The file or device must be open.

Comments:

This function gets the next sequence (one line, including '\n') of characters from a file or device. The characters will have values from 0 to 255.

If the line had an end of line marker, a ~'\n'~ terminates the line. The last line of a file needs not have an end of line marker.

After reading a line of text from the keyboard, you should normally output a \n character, (for example puts(1, '\n') ), before printing something. Only on the last line of the screen does the operating system automatically scroll the screen and advance to the next line.

When your program reads from the keyboard, the user can type Control+Z, which the operating system treats as "end of file". EOF will be returned.

Example 1:
sequence buffer 
object line 
integer fn 
 
-- read a text file into a sequence 
fn = open("my_file.txt", "r") 
if fn = -1 then 
    puts(1, "Couldn't open my_file.txt\n") 
    abort(1) 
end if 
 
buffer = {} 
while 1 do 
    line = gets(fn) 
    if atom(line) then 
        exit   -- EOF is returned at end of file 
    end if 
    buffer = append(buffer, line) 
end while 
Example 2:
object line 
 
puts(1, "What is your name?\n") 
line = gets(0)  -- read standard input (keyboard) 
line = line[1..$-1] -- get rid of \n character at end 
puts(1, '\n')   -- necessary 
puts(1, line & " is a nice name.\n") 
See Also:

getc, read_lines

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu